

//
//  NextViewController.m
//  界面动画集
//
//  Created by YYY on 2017/6/27.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

#import "NextViewController.h"

@interface NextViewController ()

@property(nonatomic,strong)UILabel *mainLabel;

@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置渐变色";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mainLabel];
    //渐变颜色
    [self gradientColor];
    // Do any additional setup after loading the view.
}

//渐变颜色
-(void)gradientColor
{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    NSInteger count = 20;
    NSMutableArray *colorsArray = [NSMutableArray array];
    NSMutableArray *locationArray = [NSMutableArray array];
    for (int i=0; i<count; i++)
    {
        UIColor *color = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
        [colorsArray addObject:(__bridge id)color.CGColor];
        [locationArray addObject:@(i/(CGFloat)count)];
    }
    gradientLayer.colors = colorsArray;
    gradientLayer.locations = locationArray;
    gradientLayer.type = kCAGradientLayerAxial;
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    gradientLayer.bounds = CGRectMake(0, 0, 400, 300);
    gradientLayer.position = CGPointMake(200, 340);
    gradientLayer.mask = _mainLabel.layer;
    
    [self.view.layer addSublayer:gradientLayer];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"colors"];
    animation.duration = 1.5;
    animation.repeatCount = HUGE;
    animation.autoreverses = YES;
    
    NSMutableArray *toValueArray = [NSMutableArray array];
    for (int i=0; i<count; i++)
    {
        UIColor *color = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
        [toValueArray addObject:(__bridge id)color.CGColor];
    }
    
    animation.toValue = toValueArray;
    [gradientLayer addAnimation:animation forKey:@"gradientLayer"];
}

-(UILabel *)mainLabel
{
    if (!_mainLabel)
    {
        _mainLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 200, 30)];
        //_mainLabel.backgroundColor = [UIColor lightGrayColor];
        _mainLabel.text = @"设置UILabel渐变色，渐变效果";
        _mainLabel.font = [UIFont systemFontOfSize:14];
        //_mainLabel.textColor = [UIColor blueColor];
    }
    return _mainLabel;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (_flag == 0)
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
